package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.constant.PublishConstants;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.PAskCoupon;
import com.ruoyi.system.domain.bo.PAskCouponBo;
import com.ruoyi.system.domain.vo.PAskCouponVo;
import com.ruoyi.system.mapper.PAskCouponMapper;
import com.ruoyi.system.service.IPAskCouponService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 咨询优惠券Service业务层处理
 *
 * @author ruoyi
 * @date 2022-07-28
 */
@RequiredArgsConstructor
@Service
public class PAskCouponServiceImpl implements IPAskCouponService {

    private final PAskCouponMapper baseMapper;

    /**
     * 查询咨询优惠券
     */
    @Override
    public PAskCouponVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询咨询优惠券列表
     */
    @Override
    public TableDataInfo<PAskCouponVo> queryPageList(PAskCouponBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PAskCoupon> lqw = buildQueryWrapper(bo);
        lqw.orderByDesc(PAskCoupon::getSort);
        Page<PAskCouponVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询咨询优惠券列表
     */
    @Override
    public List<PAskCouponVo> queryList(PAskCouponBo bo) {
        LambdaQueryWrapper<PAskCoupon> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PAskCoupon> buildQueryWrapper(PAskCouponBo bo) {
        LambdaQueryWrapper<PAskCoupon> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), PAskCoupon::getName, bo.getName());
        lqw.eq(bo.getMoney() != null, PAskCoupon::getMoney, bo.getMoney());
        lqw.eq(bo.getIsLimited() != null, PAskCoupon::getIsLimited, bo.getIsLimited());
        lqw.eq(bo.getTotal() != null, PAskCoupon::getTotal, bo.getTotal());
        lqw.eq(bo.getLastTotal() != null, PAskCoupon::getLastTotal, bo.getLastTotal());
        lqw.eq(bo.getUseType() != null, PAskCoupon::getUseType, bo.getUseType());
        lqw.eq(bo.getPrimaryKey() != null, PAskCoupon::getPrimaryKey, bo.getPrimaryKey());
        lqw.eq(bo.getMinPrice() != null, PAskCoupon::getMinPrice, bo.getMinPrice());
        lqw.eq(bo.getReceiveStartTime() != null, PAskCoupon::getReceiveStartTime, bo.getReceiveStartTime());
        lqw.eq(bo.getReceiveEndTime() != null, PAskCoupon::getReceiveEndTime, bo.getReceiveEndTime());
        lqw.eq(bo.getIsFixedTime() != null, PAskCoupon::getIsFixedTime, bo.getIsFixedTime());
        lqw.eq(bo.getUseStartTime() != null, PAskCoupon::getUseStartTime, bo.getUseStartTime());
        lqw.eq(bo.getUseEndTime() != null, PAskCoupon::getUseEndTime, bo.getUseEndTime());
        lqw.eq(bo.getDay() != null, PAskCoupon::getDay, bo.getDay());
        lqw.eq(bo.getType() != null, PAskCoupon::getType, bo.getType());
        lqw.eq(bo.getSort() != null, PAskCoupon::getSort, bo.getSort());
        lqw.eq(bo.getStatus() != null, PAskCoupon::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增咨询优惠券
     */
    @Override
    public Boolean insertByBo(PAskCouponBo bo) {
        PAskCoupon add = BeanUtil.toBean(bo, PAskCoupon.class);
        checkFixTime(bo, add);
        Map<String, Object> params = bo.getParams();
        if (ObjectUtil.isNotEmpty(params)) {
            parseTime(add, params);
        }

        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 校验使用有效期
     */
    private void checkFixTime(PAskCouponBo bo, PAskCoupon coupon) {
        String isFixedTime = bo.getIsFixedTime();
        if (PublishConstants.COMMON_NO.equals(isFixedTime)) {
            bo.setParams(null);
            coupon.setUseStartTime(null);
            coupon.setUseEndTime(null);
        }

        if (PublishConstants.COMMON_YES.equals(isFixedTime)) {
            bo.setDay(0);
            coupon.setDay(0);
        }
    }

    /**
     * 修改咨询优惠券
     */
    @Override
    public Boolean updateByBo(PAskCouponBo bo) {
        PAskCoupon update = BeanUtil.toBean(bo, PAskCoupon.class);
        checkFixTime(bo, update);
        Map<String, Object> params = bo.getParams();
        if (ObjectUtil.isNotEmpty(params)) {
            parseTime(update, params);
        }

        return baseMapper.updateById(update) > 0;
    }

    /**
     * 设置时间
     */
    private void parseTime(PAskCoupon obj, Map<String, Object> params) {
        String useStartTime = (String) params.get("useStartTime");
        String useEndTime = (String) params.get("useEndTime");
        Date startTime = DateUtils.parseDate(useStartTime);
        Date endTime = DateUtils.parseDate(useEndTime);
        obj.setUseStartTime(startTime);
        obj.setUseEndTime(endTime);
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(PAskCoupon entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除咨询优惠券
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public TableDataInfo<PAskCoupon> queryPageMyList(PAskCoupon bo, PageQuery pageQuery) {
        QueryWrapper<PAskCoupon> lqw = Wrappers.query();
        lqw.eq("p.del_flag", UserConstants.USER_NORMAL);
        lqw.like(StringUtils.isNotBlank(bo.getName()), "p.name", bo.getName());
        lqw.eq(bo.getPrimaryKey() != null, "p.primary_key", bo.getPrimaryKey());
        lqw.eq(bo.getType() != null, "p.type", bo.getType());
        lqw.eq(bo.getStatus() != null, "p.status", bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getUseType()), "p.use_type", bo.getUseType());
        Page<PAskCoupon> result = baseMapper.selectMyPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }
}
